import { isNavigationFailure, Router } from 'vue-router'
import NProgress from 'nprogress' // progress bar

NProgress.configure({ showSpinner: false }) // NProgress Configuration

export function createRouterGuards(router: Router) {
  router.beforeEach((to, from, next) => {
    NProgress.start() // start progress bar
    next()
  })

  router.afterEach((to, from, failure) => {
    if (isNavigationFailure(failure)) {
      console.error(failure)
    }
    NProgress.done() // finish progress bar
  })

  router.onError((error) => {
    console.error(error, '路由错误')
  })
}
